#!/bin/sh

# Author: Zhang Huangbin (michaelbibby <at> gmail.com)
# $Date: 2008-03-21 09:06:44 +0000 (Fri, 21 Mar 2008) $
# $Revision: 504 $

. ./conf/global
. ./conf/functions
. ./conf/openldap

# Add trap for 'Ctrl-C'.
trap "exit 255" 2

# -------------------------------------------------------------
# Use 'dialog' for configuration.
# -------------------------------------------------------------
DIALOG='dialog'
DIALOG_BACKTITLE='Mail Server Installation Wizard for OpenBSD 4.2 -release (i386)'

# Welcome message.
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Thanks for use" \
    --msgbox "\
Thanks for your use of this script for mail server.
Feedback, bug report, communication are all welcome.

Contact me if you need help about this script or OpenBSD:

    * Author:       Bibby
    * Mail/GTalk:   michaelbibby (at) gmail.com
    * MSN:          michaelbibby (at) hotmail.com

Ctrl-C will abort this wizard.

" \
    20 76

# Main features.
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Main Features" \
    --msgbox "\
The mail server contains the following main features:

* SMTP (Use Postfix, with LDAP support)
* POP3/IMAP (Use Courier POP3/IMAP)
* Virtual Domains/Users (Use OpenLDAP, with phpLDAPadmin)
* Web Mail (Use SquirrelMail, i18n support, with useful plugins)
    * Calendar, Global LDAP Address Book, Check Quota, Select Language
* Anti-Spam (Use spamd(8) in base system, SpamAssassin)
* Anti-Virus (Use ClamAV)

" \
    20 76

# PKG_PATH
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "PKG_PATH variable" \
    --inputbox "\
We will use PKG_PATH to install binary packages.
Most of packages can use binary package directly, it's recommended.

Note:

    * PKG_PATH variable MUST end in a slash (/).
    * OpenBSD official ftp site is *NOT* recommended.

EXAMPLES:

    * ftp://ftp.tw.openbsd.org/pub/OpenBSD/$(uname -r)/packages/$(machine -a)/

" \
    20 76 \
    "${PKG_PATH}" \
    2>/tmp/pkg_path

PKG_PATH="$(cat /tmp/pkg_path)"
echo "PKG_PATH='${PKG_PATH}'" >${CONFIG_FILE}
rm -f /tmp/pkg_path

# VMAIL_USER_HOME_DIR
VMAIL_USER_HOME_DIR="/home/vmail"
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "HOME directory of VMAIL user" \
    --inputbox "\
Please specify the HOME directory of vmail user: ${VMAIL_USER_NAME}.

EXAMPLE:

    * ${VMAIL_USER_HOME_DIR}

NOTE:

    * All mails will be stored in this HOME directory, so it may take
      large disk space.
" \
    20 76 "${VMAIL_USER_HOME_DIR}" \
    2>/tmp/vmail_user_home_dir

VMAIL_USER_HOME_DIR="$(cat /tmp/vmail_user_home_dir)"
echo "VMAIL_USER_HOME_DIR='${VMAIL_USER_HOME_DIR}'" >>${CONFIG_FILE}
rm -f /tmp/vmail_user_home_dir

# --------------------------------------------------
# --------------------- Backend --------------------
# --------------------------------------------------
${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Choose your prefer backend" \
    --radiolist "\
We provide two backends to store virtual domains/usersand the
homologous webmail programs:
    +----------+--------------+--------------+
    | Backend  | Web Mail     | Admin tool   |
    +----------+--------------+--------------+
    | OpenLDAP | SquirrelMail | phpLDAPadmin |
    +----------+--------------+--------------+
    | MySQL    | Roundcube    | PostfixAdmin |
    +----------+--------------+--------------+

TIP:
    Use 'Space' key to select item.
" 20 76 2 \
    "OpenLDAP" "An open source implementation of LDAP protocol. " "on" \
    "MySQL" "The world's most popular open source database." "off" \
    2>/tmp/backend

BACKEND="$(cat /tmp/backend)"
echo "BACKEND='${BACKEND}'" >> ${CONFIG_FILE}

if [ X"${BACKEND}" == X"OpenLDAP" ]; then
    . ${TOOLS_DIR}/ldap_config.sh
else
    . ${TOOLS_DIR}/mysql_config.sh
fi


${DIALOG} --backtitle "${DIALOG_BACKTITLE}" \
    --title "Specify mail alias for 'root' user" \
    --inputbox "\
Please specify a mail alias address for 'root' user:

Mail deliver failure notice will send to this alias address instead of
system account 'root'.

EXAMPLE:

    * abc@gmail.com
" \
    20 76 \
    2>/tmp/mail_alias_root

MAIL_ALIAS_ROOT=$(cat /tmp/mail_alias_root)
echo "MAIL_ALIAS_ROOT=${MAIL_ALIAS_ROOT}" >> ${CONFIG_FILE}
rm /tmp/mail_alias_root

cat <<EOF
Configuration completed.
Remember to delete configuration file after installation completed successfully.
    * ${CONFIG_FILE}
EOF
